package com;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

import org.junit.Test;

import conn.Cont;
import conn.PrepStm;
import conn.ResultSetUtil;
import conn.StatementUtil;

public class Fogin {
	@Test
	public void fogin() throws ClassNotFoundException, SQLException{
		Cont ct=new Cont();
		Connection conn=ct.getConnection();
		StatementUtil stam=new StatementUtil();
		Statement st=stam.getStatement(conn);
		ResultSetUtil rsu=new ResultSetUtil();
		String sql=" select * from login where id='"+1+"';";
		ResultSet rs=rsu.getResult(st, sql);
		
		
		
		while(rs.next()){
			
			String name=rs.getString(2);
			String password=rs.getString(3);
			boolean bl=new Fogin().vali(name, password);
			 if(bl=true){
				 break;
			 }
			}
		
		
		conn.close();
		st.close();
		rs.close();
	}
	@Test
	public void foginp() throws ClassNotFoundException, SQLException{
		Cont ct=new Cont();
		Connection conn=ct.getConnection();
		String sql=" select * from login where id='"+1+"';";
		PrepStm psm=new PrepStm();
		PreparedStatement st=psm.getPreparedStatement(conn,sql);
		ResultSetUtil rsu=new ResultSetUtil();
		
		ResultSet rs= rsu.getResult(st, sql);
		while(rs.next()){
			
			String name=rs.getString(2);
			String password=rs.getString(3);
			boolean bl=new Fogin().vali(name, password);
			 if(bl=true){
				 break;
			 }
			}
		/*String[] name=new String[100];
		String[] password=new String[100];
		int i=0;
		while(rs.next()){
			System.out.println(rs.getString(2)+"\t"+rs.getString(3));
			 name[i]=rs.getString(2);
			 password[i]=rs.getString(3);
			 i++;
			
			}
		for(int j=0;j<3;j++){
		boolean bl=new Fogin().vali(name[j], password[j]);
		
		 if(bl=true){
			 break;
		 }
	}*/
		conn.close();
		st.close();
		rs.close();
	}
	
	public boolean vali(String name,String password){
		boolean temp=false;
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入账号名");
		String username=sc.next();
		System.out.println("请输入密码名");
		String userpwd=sc.next();
		if(name.equals(username)&&password.equals(userpwd)){
			System.out.println("欢迎登陆");
			temp=true;
		}else{
			System.out.println("登陆失败，请重新登陆");
			new Fogin().vali(name, password);
		}
			
		
		return temp;
		
		
		
	}

}
